<?

/*		OBJECT
**		
**		MODULE vars:
**		stdClass Object ( [limit] => 3 ) 
*/


class SocialLastfm extends MOD {
	
	function __construct( $vars ) {
		
		$this->_addvars( $vars );
		$this->_ref();
	}
	
	
	public function output () {
		global $Engine;

		// 1. load LASTFM class>object
		$this->lastfm = new LASTFM();

		// 2. check action-functions
		if( $this->action ) $result = $this->{ 'filter_' . $this->action }();
		
		// 3. final response
		if ( $this->success === false ) return '';
		$result = "\n<ul>\n" . implode( "\n", $result ) . "\n</ul>\n" . $this->render_js();
		return $this->parse ( $result );
		
		
		
	}
	

#	ACTION: Preview_tracks
	private function filter_preview_tracks () {
		global $Engine;
		
		# no output for IE
		if ( IE ) { 
			$this->success = false;
			return array ( 'empty' );
		}
			
		if( $_GET['id'] && $key = $this->get_artist_key() ) {
			
			// 1: retrieve most popular tracks ( from $this->lastfm )
			// $type, $value, $method: ('artist', 'Artist Name', 'getTopTracks' )
			$data = $this->lastfm->get_data( 'artist', urldecode( $key ), 'getTopTracks' );
			
			// 2: create YouTube connector
			$this->YT = new YOUTUBE();
			
			// 3: retrieve & return results
			$emptyItems = 0;
			for ( $i=0; $i < $this->vars->limit; $i++ ) {
				
				// check if for some reason the track name is empty
				// if so, increase number of emptyItems by one
				if ( $data[$i]['name'] == '' ) $emptyItems++;
				
				if ( ( $id = $this->filter_youtube_id( $data[$i]['name'] ) ) && ( $data[$i]['name'] != '' ) )
					$results[] = "<li class='tag' ref='". $id ."'>" . $data[$i]['name'] . "</li>";
			}
			
			// if only empty results were returned, don't output them
			if ( !$results ) { 
				$this->success = false;
				return array ( 'empty' );
			}
			
			$this->js = "
				mod = new SocialLastfm( 'toptracks' );
				mod.target = '.movie';
				mod.source = 'http://www.youtube.com/v/';
			";
			
			$media = new MEDIA( array( 'width'=>$this->vars->ytwidth, 'height'=>$this->vars->ytheight ));
			$results[] = "<li class='movie'>" . $media->_flash( array( 'src'=>"" )) . "</li>";
			
			return $results;
		}
		
		else {
			$this->success = false; 
			return array( 'empty.' );
		}
	}
	
	
	private function filter_youtube_id( $string ) {
		
		// retrieve data: ( 'Artist Name, Track Name', Category, maxResults, startIndex, orderBy )
		$this->YT->request( array( $this->get_artist_key(), $string ), 'Music', 10, 1, 'relevance' );
		
		// embeddable videos only: return id of first embeddable video
		if ( $videos = $this->YT->filter( 'id', 'embedAllowed' ) )  return $this->get_embeddable_video( $videos );
	}
	
	
	private function get_artist_key() {
		global $Engine, $Object;
		
		// retrieve object
		$artist = $Engine->object->get( 'band', $_GET['id'] );
		
		// return key from meta json, else return artist name as key
		return ( $key = json_decode( $artist['meta'] )->lastfm_key ) ? $key :  $Object->get( 'name' );
	}
	
	
	// return the YouTube video id of first embeddable video
	private function get_embeddable_video( $videos ) {
	
		foreach( $videos as $vid ) if( $vid['embedAllowed'] == 'allowed' ) return $vid['id'];
	}
	
	
	private function filter_recent() {
		
		$user_data = simplexml_load_file( 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=' . $this->vars->user . '&api_key=b25b959554ed76058ac220b7b2e0a026' );
		foreach ( $user_data->recenttracks->track as $track ) 
			$result[] = '<li class="track"><span class="artist">' . $track->artist . '</span><span class="name">' . $track->name . '</span></li>';
		
		return $result;
		
	}
	
	
}


?>



